package com.example.service.Impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.exception.CustomException;
import com.example.mapper.WarehouseMapper;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Material;
import com.example.pojo.entity.User;
import com.example.pojo.entity.Warehouse;
import com.example.service.IWarehouseService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库信息表 服务实现类
 *
 * @author wy
 * @since 2024-07-17
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public void add(Warehouse warehouse) {
        warehouse.setIsDeleted(0);
        warehouse.setVersion(0);
        warehouseMapper.insert(warehouse);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Integer id) {
        Warehouse warehouse = warehouseMapper.selectById(id);
        warehouse.setIsDeleted(1);
        updateById(warehouse);
    }

    @Override
    public Page<Warehouse> selectPage(Warehouse warehouse, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<Warehouse> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Warehouse.class)
                .selectAs(Admin::getName, Warehouse::getUserName)
                .innerJoin(Admin.class,Admin::getId,Warehouse::getUserId)
                .eq(Warehouse::getIsDeleted,0)
                .eq(ObjectUtil.isNotNull(warehouse.getName()) ,Warehouse::getName , warehouse.getName());

        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }
}
